package lol.clann.bukkit.pluginboot.model.gui;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;

import java.util.function.Function;

/**
 * 映射为一个格子
 *
 * @author pyz
 * @date 2018/6/30 下午9:50
 */
public class Element<S extends IconSupplier> implements IconSupplier {

    private S extend;

    @JsonIgnore
    private Function<ClickAction<InventoryClickEvent, S>, Boolean> clickAction;

    private Function<S, ItemStack> icon = (s) -> s.getIcon();

    /**
     * 供反序列化调用
     * clickAction需要手动赋值
     */
    public Element() {

    }

    public Function<ClickAction<InventoryClickEvent, S>, Boolean> getClickAction() {
        return clickAction;
    }

    public Element<S> setClickAction(Function<ClickAction<InventoryClickEvent, S>, Boolean> clickAction) {
        this.clickAction = clickAction;
        return this;
    }

    /**
     * 返回包裹的对象
     *
     * @return
     */
    public S getExtend() {
        return extend;
    }

    public Element<S> setExtend(S extend) {
        this.extend = extend;
        return this;
    }

    @Override
    public ItemStack getIcon() {
        return icon.apply(getExtend());
    }

    public Element<S> setIcon(Function<S, ItemStack> icon) {
        this.icon = icon;
        return this;
    }

}
